﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class StopInstanceSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        string wiid, ctlId, iid, wid, userid, step;
        TextBox whystop;
        Button stopbtn;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
        }

        private void InitControl()
        {
            wiid = base.Page.Request["wiid"];
            ctlId = HttpContext.Current.Server.UrlDecode(base.Page.Request["CTLID"]);
            iid = base.Page.Request["iid"];
            wid = _FlowFactoryProvider.GetWidByIID(iid);
            userid = UserId;
            step = _FlowFactoryProvider.GetStepName(ctlId,wid);

            whystop = (TextBox)FindControl("whystop");
            stopbtn = (Button)FindControl("stopbtn");
            stopbtn.Click += stopbtn_Click;
            bool canopen = _FlowFactoryProvider.CanOpen(userid, iid, wiid, step);
            bool isopen = _FlowFactoryProvider.IsOpen(iid, wiid, step);
            //if (!canopen || isopen)
            //{
            //    Antu.Utilities.UtilitiesPage.AlertDialogClear("该业务已被处理或无权限处理！", "window.parent.closeDialog();");
            //    return;
            //}
            string msg = "";
            if (false == _FlowFactoryProvider.TaskOpenCheck(iid, wiid, step, userid, out msg))
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("alert('" + msg + "！');", "window.parent.closeDialog();");
                return;
            }
        }

        private void stopbtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(whystop.Text))
            {
                bool isok = _FlowFactoryProvider.StopInstance(iid, wiid, userid, whystop.Text);
                if (isok)
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("成功中止流程！", "window.parent.reloadandclose();");
                }
                else
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("中止失败！", "history.back();");
                }
            }
            else
            {
                Antu.Utilities.UtilitiesPage.AlertDialog("中止原因不得为空！", "history.back();");
            }
        }

        private void Initcy()
        {

        }


    }
}


